package edu.arizona.sista.learn.activity;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Switch;

import edu.arizona.sista.learn.activity.experiment.RecognitionExperiment;
import edu.arizona.sista.learn.activity.fluents.paths.PredicateExtractor;
import edu.arizona.sista.learn.activity.fluents.paths.Scene;
import edu.arizona.sista.learn.activity.model.ActivityInstance;
import edu.arizona.sista.learn.activity.model.Predicate;
import edu.arizona.sista.learn.activity.model.PredicateInterval;

public class Main {

	public static void main(String[] args) throws Exception {
		
		JSAP jsap = new JSAP();
		Switch helpSwitch = new Switch("help")
				.setShortFlag('h')
				.setLongFlag("help");
		FlaggedOption root = new FlaggedOption("movies-root")
				.setDefault("")
				.setRequired(true)
				.setLongFlag("movies-root");
		FlaggedOption tracks = new FlaggedOption("tracks-subdir")
				.setDefault("points")
				.setRequired(false)
				.setLongFlag("tracks-subdir");
		FlaggedOption type = new FlaggedOption("tracks-type")
			.setDefault("heatmaps")
			.setRequired(false)
			.setLongFlag("tracks-type");
		jsap.registerParameter(helpSwitch);
		jsap.registerParameter(root);
		jsap.registerParameter(tracks);
		jsap.registerParameter(type);

		JSAPResult config = jsap.parse(args);
		if (config.getBoolean("help") || "".equals(config.getString("movies-root"))) {
			System.out.print("java -jar activity.jar ");
			System.out.println(jsap.getUsage());
			return;
		}
		File dir = new File(config.getString("movies-root"));
		if (!dir.isDirectory()) {
			System.err.println("Invalid input directory!");
			return;
		}
		String pointsSubdir = config.getString("tracks-subdir");
		String tracksType = config.getString("tracks-type");
		
		// Iterate over each movie
		int counter = 0;
		for (File child : dir.listFiles()) {
			if (child.getName().startsWith(".")) {
		    	continue;  // Ignore the special files.
		    }

			try {
				
				// Extract fluents and generate activity instance
				Scene scene = Scene.load(child.getPath() + "/" + pointsSubdir + "/");
				PredicateExtractor extractor;
				if (tracksType.equalsIgnoreCase("heatmaps")) {
					extractor = new PredicateExtractor(scene, PredicateExtractor.DataType.Heatmaps);
				} else {
					extractor = new PredicateExtractor(scene, PredicateExtractor.DataType.Points);
				}
				Vector<Vector<Predicate>> preds = extractor.extractPredicates();
				ActivityInstance instance = extractor.getPredicateIntervals(preds);
				
				// Dump activity instance
				System.out.println(instance.toString());
				PrintStream out1 = new PrintStream(child.getPath() + "/fluents.lisp");
				out1.println(instance.toString());
				out1.close();
				
				// Run recognizers
				Map<String, List<PredicateInterval>> results = RecognitionExperiment.recognition(instance);
				
				// Dump recognition results
				PrintStream out2 = new PrintStream(child.getPath() + "/activities.txt");
				for (String key : results.keySet()) {
					List<PredicateInterval> list = results.get(key);
					for (PredicateInterval i : list) {
						out2.println(i.getPredicate().toString() + " " + i.start + " " + i.end);
					}
				}
				out2.close();
				
				counter++;
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		    
		}
		
		System.out.println("Successfully processed " + counter + " video(s).");
	}
}
